package com.study.services;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.TransferJDao;
import com.study.model.mdao.InboundMapper;
import com.study.model.mdao.InventoryMapper;
import com.study.model.mdao.OutboundMapper;
import com.study.model.mdao.ProductMapper;
import com.study.model.mdao.TransferMapper;
import com.study.model.pojo.Inventory;
import com.study.model.pojo.Transfer;

@Service
public class TransferService {
	@Autowired
	TransferMapper tMapper;
	@Autowired
	ProductMapper pMapper;
	@Autowired
	InventoryMapper iMapper;
	@Autowired
	OutboundMapper oMapper;
	@Autowired
	InboundMapper inMapper;
	@Autowired
	TransferJDao tDao;

	// 查询所有调库单
	public List<Transfer> findAll() {
		return tMapper.findAll();
	}

	/**
	 * 新增调库信息
	 * @param map 新增调库信息
	 * @param proId 调库产品id
	 * @param transfer 调库信息
	 * @param outKind 调库方式
	 * @param inbUnit 调库产品单位
	 */
	public String addTransfer(HashMap map, String proId, Transfer transfer, String outKind, String inbUnit) {
		int i = pMapper.findexist(proId);// 是否产品存在
		String returnString;
		if (i>0) {
			tMapper.addTransfer(map);
			returnString="y";
			return returnString;
		}else {
			returnString="n";
			return returnString;
		}
	}
	
	//审核通过
	public void pass(String traId) {
		tMapper.pass(traId);
		Transfer transfer=tDao.findBytraId(traId);
		HashMap map=new HashMap<>();
		map.put("traId", traId);
		map.put("traStatic", transfer.getTraStatic());
		int i=tMapper.exit(map);
		if (i>0) {
			HashMap mapOutfind = new HashMap<>();
			mapOutfind.put("proId", transfer.getProduct().getProId());
			mapOutfind.put("warName", transfer.getTraOut());
			int z1 = iMapper.findByproIdAndwarName(mapOutfind);// 根据产品id与仓库名称查询产品是否存在于库存表
			// 如果存在，新增出库
			if (z1 > 0) {
				// 新增出库
				HashMap mapOutbound = new HashMap<>();
				mapOutbound.put("outId", "out0008");
				mapOutbound.put("proId", transfer.getProduct().getProId());
				mapOutbound.put("saleId", traId);
				mapOutbound.put("warName", transfer.getTraOut());
				mapOutbound.put("intName", "调拨出库");
				mapOutbound.put("outNum", transfer.getProNum());
				mapOutbound.put("outKind", "自费");
				mapOutbound.put("outUse", "");
				mapOutbound.put("outNote", "调拨出库,自动生成");
				mapOutbound.put("outStatic", "已审核");
				System.out.println("出库信息map: " + mapOutbound);
				oMapper.addOutbound(mapOutbound);// 新增出库信息
				int j1 = iMapper.findForinvNum(mapOutfind).getInvNum();// 查询符合条件产品的库存数量
				int j2 = iMapper.findForinvNum(mapOutfind).getInvHeight();// 查询符合条件产品的挤压库存预警数量
				int j3 = iMapper.findForinvNum(mapOutfind).getInvLow();// 获取符合条件产品的低库存预警数
				int num = transfer.getProNum();
				j1 = j1 - num;
				System.out.println("减去的数等于:" + j1 + "----点酷暑" + num);
				HashMap mapinv = new HashMap<>();
				mapinv.put("proId", transfer.getProduct().getProId());
				mapinv.put("invNum", j1);
				mapinv.put("warName", transfer.getTraOut());
				System.out.println("查询到的库存数量" + j1 + "----修改库存信息" + mapinv);
				iMapper.editinvNum(mapinv);// 修改库存
			}
			HashMap mapinfind = new HashMap<>();
			mapinfind.put("proId", transfer.getProduct().getProId());
			mapinfind.put("warName", transfer.getTraIn());
			int z2 = iMapper.findByproIdAndwarName(mapinfind);// 根据产品id与仓库名称查询产品是否存在于库存表
			// 新增入库
			HashMap mapInbound = new HashMap<>();
			mapInbound.put("inbId", "inb0008");
			mapInbound.put("proId", transfer.getProduct().getProId());
			mapInbound.put("orderId", traId);
			mapInbound.put("warName", transfer.getTraIn());
			mapInbound.put("intName", "调拨入库");
			mapInbound.put("inbUnit", transfer.getProduct().getProUnit());
			mapInbound.put("inbNum", transfer.getProNum());
			mapInbound.put("inbNote", "调拨入库,自动生产");
			mapInbound.put("inbStatic", "已审核");
			mapInbound.put("inbSupplier", "");
			inMapper.addInbound(mapInbound);// 新增入库信息
			// 如果入库信息在库存表中存在，直接修改库存信息，如果不存在，新增库存
			if (z2 > 0) {
				int j1 = iMapper.findForinvNum(mapInbound).getInvNum();// 查询符合条件产品的库存数量
				int j2 = iMapper.findForinvNum(mapInbound).getInvHeight();// 查询符合条件产品的挤压库存预警数量
				int j3 = iMapper.findForinvNum(mapInbound).getInvLow();// 获取符合条件产品的低库存预警数
				int num = transfer.getProNum();
				j1 = j1 + num;
				System.out.println("相加的数等于:" + j1 + "----点酷暑" + num);
				HashMap mapinv = new HashMap<>();
				mapinv.put("proId", transfer.getProduct().getProId());
				mapinv.put("invNum", j1);
				mapinv.put("warName", transfer.getTraIn());
				System.out.println("查询到的库存数量" + j1 + "----修改库存信息" + mapinv);
				iMapper.editinvNum(mapinv);// 修改库存
			} else {
				HashMap mapInventory = new HashMap<>();
				mapInventory.put("invId", "inv0010");
				mapInventory.put("proId", transfer.getProduct().getProId());
				mapInventory.put("warName", transfer.getTraIn());
				mapInventory.put("invUnit", transfer.getProduct().getProUnit());
				mapInventory.put("invLow", 0);
				mapInventory.put("invHeight", 0);
				mapInventory.put("invNum", transfer.getProNum());
				mapInventory.put("invStatic", "正常");
				mapInventory.put("invNote", "");
				iMapper.addInventory(mapInventory);// 新增库存信息
				tMapper.addTransfer(map);//新增调拨记录
			}
		}
	}
}
